#include "logwriter.h"

using namespace std;

logwriter::logwriter(void)
{
	this->filePath = "log.xml";
    root = new TiXmlElement( "SceneView_Log" );  
	doc.LinkEndChild( root );  
}

logwriter::logwriter(string filename)
{
	this->filePath = filename;
    root = new TiXmlElement( "SceneView_Log" );  
	doc.LinkEndChild( root );  
}


logwriter::logwriter(string filename, string logName, bool appendIfExists)
{
	this->filePath = filename;
	if(appendIfExists)
	{
		bool exists = doc.LoadFile(filename.data());
		if(!exists)
		{
			doc.Clear();
		}
	}
	root = new TiXmlElement( logName.data() );  
	doc.LinkEndChild( root );  
}

void logwriter::close(void)
{
	doc.SaveFile(filePath.data());
}

void logwriter::writeEntry(string entryName)
{
	TiXmlElement * entry = new TiXmlElement("logEntry");  

		TiXmlElement *entryNameNode = new TiXmlElement("entryName");  
		entryNameNode->LinkEndChild( new TiXmlText(entryName.data()) );
		entry->LinkEndChild( entryNameNode );  

	root->LinkEndChild( entry );  
}


void logwriter::writeEntry(string entryName, string value)
{
	TiXmlElement * entry = new TiXmlElement("logEntry");  

		TiXmlElement *entryNameNode = new TiXmlElement("entryName");  
		entryNameNode->LinkEndChild( new TiXmlText(entryName.data()) );
		entry->LinkEndChild( entryNameNode );  

		TiXmlElement *entryValueNode = new TiXmlElement("entryValue"); 
		entryValueNode->LinkEndChild( new TiXmlText(value.data()) );
		entry->LinkEndChild( entryValueNode );  

	root->LinkEndChild( entry );  
}

void logwriter::writeEntry(string entryName, float value)
{
	TiXmlElement * entry = new TiXmlElement("logEntry");  

		TiXmlElement *entryNameNode = new TiXmlElement("entryName");  
		entryNameNode->LinkEndChild( new TiXmlText(entryName.data()) );
		entry->LinkEndChild( entryNameNode );  

		TiXmlElement *entryValueNode = new TiXmlElement("entryValue"); 

		ostringstream s;
		s << value;

		entryValueNode->LinkEndChild( new TiXmlText(s.str().data()) );
		entry->LinkEndChild( entryValueNode );  

	root->LinkEndChild( entry );  
}

void logwriter::writeEntry(string entryName, float value1, float value2, float value3)
{
	
	TiXmlElement * entry = new TiXmlElement("logEntry");  

		TiXmlElement *entryNameNode = new TiXmlElement("entryName");  
		entryNameNode->LinkEndChild( new TiXmlText(entryName.data()) );
		entry->LinkEndChild( entryNameNode );  


		TiXmlElement *entryValueNode1 = new TiXmlElement("entryValue"); 

		ostringstream s1;
		s1 << value1;

		entryValueNode1->LinkEndChild( new TiXmlText(s1.str().data()) );
		entry->LinkEndChild( entryValueNode1 );  


		TiXmlElement *entryValueNode2 = new TiXmlElement("entryValue"); 
		
		ostringstream s2;
		s2 << value2;

		entryValueNode2->LinkEndChild( new TiXmlText(s2.str().data()) );
		entry->LinkEndChild( entryValueNode2 );  


		TiXmlElement *entryValueNode3 = new TiXmlElement("entryValue"); 

		ostringstream s3;
		s3 << value3;

		entryValueNode3->LinkEndChild( new TiXmlText(s3.str().data()) );
		entry->LinkEndChild( entryValueNode3 );  

	root->LinkEndChild( entry );  
}

void logwriter::writeEntry(string entryName, float value1, float value2, float value3, float value4)
{
	
	TiXmlElement * entry = new TiXmlElement("logEntry");  

		TiXmlElement *entryNameNode = new TiXmlElement("entryName");  
		entryNameNode->LinkEndChild( new TiXmlText(entryName.data()) );
		entry->LinkEndChild( entryNameNode );  


		TiXmlElement *entryValueNode1 = new TiXmlElement("entryValue"); 

		ostringstream s1;
		s1 << value1;

		entryValueNode1->LinkEndChild( new TiXmlText(s1.str().data()) );
		entry->LinkEndChild( entryValueNode1 );  


		TiXmlElement *entryValueNode2 = new TiXmlElement("entryValue"); 
		
		ostringstream s2;
		s2 << value2;

		entryValueNode2->LinkEndChild( new TiXmlText(s2.str().data()) );
		entry->LinkEndChild( entryValueNode2 );  


		TiXmlElement *entryValueNode3 = new TiXmlElement("entryValue"); 

		ostringstream s3;
		s3 << value3;

		entryValueNode3->LinkEndChild( new TiXmlText(s3.str().data()) );
		entry->LinkEndChild( entryValueNode3 );  


		TiXmlElement *entryValueNode4 = new TiXmlElement("entryValue"); 

		ostringstream s4;
		s4 << value4;

		entryValueNode4->LinkEndChild( new TiXmlText(s4.str().data()) );
		entry->LinkEndChild( entryValueNode4 );  

	root->LinkEndChild( entry );  
}